﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MonsterAI : IAIBase
{
    private int targetId = 0;

    public MonsterAI(OwnerObj ownerObj) : base(ownerObj)
    {
        Debug.Log("monster AI start!");
    }

    public override void Update(float deltaTime)
    {
        // 查找其他怪物，锁定一个怪物后行走到怪物附近然后攻击
        if (!ownerObj.Is<MonsterObject>())
        {
            return;
        }

        MonsterObject monsterObj = ownerObj.Get<MonsterObject>();

        if (targetId != 0)
        {
            // 当前目标是塔，先取消
            if (monsterObj.GetStage().GetObj<TowerObject>(targetId) != null)
            {
                targetId = 0;
            }
        }

        if (0 == targetId)
        {
            if (0 == targetId)
            {
                List<MonsterObject> unitMonsterObjs = monsterObj.GetStage().GetObjs<MonsterObject>();

                // 锁定距离最近的
                float minDis = 9999;
                MonsterObject targetMonsterObj = null;

                foreach (MonsterObject findMonsterObj in unitMonsterObjs)
                {
                    if (findMonsterObj.IsDead())
                    {
                        continue;
                    }

                    if (findMonsterObj.GetModule().Get<ModAttr>().GetGroup() != monsterObj.GetModule().Get<ModAttr>().GetGroup()
                        && findMonsterObj.GetId() != monsterObj.GetId())
                    {
                        // 锁定怪物
                        float twoDis = Vector3.Distance(findMonsterObj.GetPosition(), monsterObj.GetPosition());
                        if (twoDis <= minDis)
                        {
                            minDis = twoDis;
                            targetMonsterObj = findMonsterObj;
                        }
                    }
                }

                if (targetMonsterObj != null)
                {
                    targetId = targetMonsterObj.GetId();
                }
            }

            if (0 == targetId)
            {
                // 锁定塔
                List<TowerObject> unitTowerObjs = monsterObj.GetStage().GetObjs<TowerObject>();

                foreach (TowerObject towerObj in unitTowerObjs)
                {
                    if (towerObj.IsDead())
                    {
                        continue;
                    }

                    if (towerObj.GetModule().Get<ModAttr>().GetGroup() != monsterObj.GetModule().Get<ModAttr>().GetGroup()
                        && towerObj.GetId() != monsterObj.GetId())
                    {
                        targetId = towerObj.GetId();
                    }
                }
            }
        }
        
        if (0 != targetId)
        {
            IUnitObject targetUnitObj = monsterObj.GetStage().GetObj<IUnitObject>(targetId);
            if (null != targetUnitObj)
            {
                if (targetUnitObj.IsDead())
                {
                    // 丢失目标
                    targetId = 0;
                    return;
                }

                // 获取位置
                if (Vector3.Distance(monsterObj.GetPosition(), targetUnitObj.GetPosition()) > 1.5)
                {
                    // 移动
                    monsterObj.GetModule().Get<ModAction>().Move(targetUnitObj.GetPosition());
                }
                else
                {
                    // 攻击
                    monsterObj.GetModule().Get<ModAction>().Attack(targetId);
                }
            }
        }

    }
}
